package cn.sinjet.myview;

import com.mgroup.wazehelper.R;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class DoorOpenWarningView extends Dialog{
	ViewGroup mViewRoot;
//	private View mMainOsdView = null;
	private Context context;
	boolean isShown = false;
	ImageView mLeftFront,mRightFront,mLeftRear,mRightRear,mTrunk;
    public DoorOpenWarningView(Context context,int theme){
    	super(context,theme);
    	this.context = context;
		
        /*mMainOsdView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View arg0, MotionEvent motionEvent) {
				// TODO Auto-generated method stub
				Log.d("osd", "onTouchEvent:"+motionEvent.getAction());
				if(motionEvent.getAction() == MotionEvent.ACTION_DOWN)
					hideDialogView();
				return false;
			}
		});*/
//		wm.addView(mMainOsdView, params);
//		initViews(mMainOsdView);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	// TODO Auto-generated method stub
    	super.onCreate(savedInstanceState);
    	mViewRoot = (ViewGroup)LayoutInflater.from(context).inflate(R.layout.layout_door_warning,null);
    	setContentView(mViewRoot);
    	initViews(mViewRoot);
    }    
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	// TODO Auto-generated method stub
    	hideDialogView();
    	return super.onTouchEvent(event);
    }
    void initViews(View view){
    	mLeftFront = (ImageView)view.findViewById(R.id.door_left_front);
    	mRightFront = (ImageView)view.findViewById(R.id.door_right_front);
    	mLeftRear = (ImageView)view.findViewById(R.id.door_left_rear);
    	mRightRear = (ImageView)view.findViewById(R.id.door_right_rear);
    	mTrunk = (ImageView)view.findViewById(R.id.door_trunk);
    }
    
    public void showOsdView(int leftFront,int rightFront,int leftRear,int rightRear,int trunk){
    	if(leftFront==0&& rightFront==0&&leftRear==0&&rightRear==0&&trunk==0){
    		hideDialogView();
    		return;
    	}
//    	Log.d("view", "showOsdView:"+volume);
    	mHandler.removeCallbacks(dispearRunnable);
    	mHandler.postDelayed(dispearRunnable, 10000);
        if(isShown) {
            updateOsdView(leftFront,rightFront,leftRear,rightRear,trunk);	
        	return;
        }
//    	if(wm !=null && mMainOsdView !=null){
////    		params.y = 300;
//    		wm.addView(mMainOsdView, params);
    		
////    		wm.updateViewLayout(mMainOsdView, params);
//    	}
    	isShown = true;
    	DoorOpenWarningView.this.show();
    	updateOsdView(leftFront,rightFront,leftRear,rightRear,trunk);
//    	wm.up
    }
    
    void updateOsdView(int leftFront,int rightFront,int leftRear,int rightRear,int trunk){
    	mLeftFront.setVisibility((leftFront==0)?View.INVISIBLE:View.VISIBLE);
    	mRightFront.setVisibility((rightFront==0)?View.INVISIBLE:View.VISIBLE);
    	mLeftRear.setVisibility((leftRear==0)?View.INVISIBLE:View.VISIBLE);
    	mRightRear.setVisibility((rightRear==0)?View.INVISIBLE:View.VISIBLE);
    	mTrunk.setVisibility((trunk==0)?View.INVISIBLE:View.VISIBLE);
    }
    
    Handler mHandler = new Handler();
    Runnable dispearRunnable = new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			hideDialogView();
		}
	};
    
    public boolean isShown(){
    	return isShown;
    }
    private void hideDialogView(){
	   if(isShown && this.isShowing()) 
		   this.dismiss();
    	isShown = false;
    }
       
}
